var state = app.getState();
var uid = state.user_id;
var pages = ["order.html", "transaction.html", "wallet.html", "setting.html","index.html"];
/*
var subStyle = {
	top: '45px',
	bottom: '50px'
};*/
var subStyle = {
	top: '0',
	bottom: '50px'
};
var objSub = [];
for(var i = 0; i < pages.length; i++) {
	//创建多个窗口
	var sub = {
		url: pages[i],
		id: pages[i],
		styles: subStyle
	};
	
	objSub.push(sub);
}

mui.init({
	swipeBack: false,
	//beforeback: back,
	subpages: objSub
});
//创建窗体对象，把子窗口依次放入
mui.plusReady(function() {	
    plus.screen.lockOrientation("portrait-primary");
    
    //alert(11111);
	var backButtonPress = 0;
	mui.back = function() {
		backButtonPress++;
		if(backButtonPress > 1) {
			plus.runtime.quit();
		} else {
			plus.nativeUI.toast('再按一次退出应用');
		}
		setTimeout(function() {
			backButtonPress = 0;
		}, 1000);
	}
	var mainWin = plus.webview.currentWebview();
	localStorage.setItem('mainpage', mainWin.id);
	localStorage.setItem('defaultTab', 'index.html');

	function switchpage(_event, targetTab) {
		var activeTab = localStorage.getItem('defaultTab');
		var activeObj = document.getElementById(activeTab);
		var targetObj = document.getElementById(targetTab);
		plus.webview.hide(activeTab);
		//显示目标选项卡
		plus.webview.show(targetTab, "fade-in", 300);
		//title.innerHTML = document.getElementById('label' + targetTab).innerHTML;

		//switchPage("tap", activeTab, targetTab);
		//更改当前活跃的选项卡
		if(_event == "switch") {
			// 底部选项
			activeObj.classList.remove("mui-active");
			targetObj.classList.add("mui-active");
		}

		localStorage.setItem('defaultTab', targetTab);
		
		/*
		if(activeTab == 'index.html') {
			var chatWin = plus.webview.getWebviewById('index.html');
			//console.log(JSON.stringify(chatWin));
			mui.fire(chatWin, 'refreshList', null);
		}
        */
	}

	mui('.mui-bar-tab').on('tap', 'a', function(e) {
		var targetTab = this.getAttribute('id');
		var activeTab = localStorage.getItem('defaultTab');
		
		if(targetTab != activeTab) {
			//console.log(targetTab);
			//switchPage("tap", activeTab, targetTab);
			switchpage("tap", targetTab);
		}

	});

	// 子页面滑动切换事件
	window.addEventListener("swipe_event", function(event) {
		// 获取方向以及索引
		var target = event.detail.target;
		switchpage("switch", target);

	});
});
